package com.arcgis.app;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.media.Image;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.arcgis.app.RecyclerView.SpacesItemDecoration;
import com.arcgis.app.adapter.ListMessageAdapter;
import com.arcgis.app.controller.fragment.FragmentTool;
import com.arcgis.app.controller.fragment.PointListFragment;
import com.arcgis.app.dao.PointInfoDao;
import com.arcgis.app.listener.GeoTrackTouchListener;
import com.arcgis.app.utils.BitMapWorkTask;
import com.arcgis.app.utils.Urls;
import com.bumptech.glide.Glide;
import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
import com.esri.arcgisruntime.geometry.Envelope;
import com.esri.arcgisruntime.geometry.Geometry;
import com.esri.arcgisruntime.geometry.GeometryEngine;
import com.esri.arcgisruntime.geometry.GeometryType;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.Polygon;
import com.esri.arcgisruntime.geometry.Polyline;
import com.esri.arcgisruntime.geometry.SpatialReference;
import com.esri.arcgisruntime.geometry.SpatialReferences;
import com.esri.arcgisruntime.layers.ArcGISTiledLayer;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.Viewpoint;
import com.esri.arcgisruntime.mapping.view.Callout;
import com.esri.arcgisruntime.mapping.view.DefaultMapViewOnTouchListener;
import com.esri.arcgisruntime.mapping.view.Graphic;
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.mapping.view.SketchCreationMode;
import com.esri.arcgisruntime.mapping.view.SketchEditor;
import com.esri.arcgisruntime.symbology.PictureMarkerSymbol;
import com.esri.arcgisruntime.symbology.SimpleFillSymbol;
import com.esri.arcgisruntime.symbology.SimpleLineSymbol;
import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

//import android.graphics.Point;
import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.centerImage)
    ImageView centerImage;
    private MapView mMapView;
    private static final String sTag = "Gesture";
    private Callout mCallout;
    Point wgs84Point;
    private RecyclerView recyclerView;
    ArrayList<PointInfoDao> listData;
    ListMessageAdapter listMessageAdapter;
    public GraphicsOverlay curPosOverlay;
    private SketchEditor sketchEditor;
    FragmentTool fragmentTool;
    PointInfoDao currentPoint;
    private AlertDialog.Builder builder;

    GraphicsOverlay pointOverLay;
    GraphicsOverlay lineOverLay;
    GraphicsOverlay polygonOverLay;

    private SimpleFillSymbol  mFillSymbol;
    private SimpleLineSymbol mLineSymbol;
    private int currentType;

    public  MapView getMap(){
        return mMapView;
    }

    public GraphicsOverlay getPointOverLay() {
        if (pointOverLay == null) {
            pointOverLay = new GraphicsOverlay();
        }
        return pointOverLay;
    }

    public GraphicsOverlay getLineOverLay() {
        if (lineOverLay == null) {
            lineOverLay = new GraphicsOverlay();
        }
        return lineOverLay;
    }

    public GraphicsOverlay getPolygonOverLay() {
        if (polygonOverLay == null) {
            polygonOverLay = new GraphicsOverlay();
        }
        return polygonOverLay;
    }

    public String selectType = "point";


    //设置地图坐标系
    private final SpatialReference wgs84 = SpatialReference.create(4326);
    GraphicsOverlay mGraphicsOverlay = new GraphicsOverlay();

    public void setSelectType(String selectType) {
        this.selectType = selectType;
    }

    public SketchEditor getSketchEditor() {

        if (sketchEditor == null) {

            sketchEditor = new SketchEditor();
        }
        return sketchEditor;
    }

    public FragmentTool getFragmentTool() {
        if (fragmentTool == null) {
            fragmentTool = (FragmentTool) getSupportFragmentManager().findFragmentById(R.id.fragment3);
        }
        return fragmentTool;
    }

    public GraphicsOverlay getCurPosOverlay() {

        if (curPosOverlay == null) {

            curPosOverlay = new GraphicsOverlay();

            curPosOverlay.setPopupEnabled(false);
        }

        return curPosOverlay;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
//        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        initMap();
        initial();
        currentType = 0;
        selectNetworkData(0);


        mLineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID,Color.BLUE,4);
        mFillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.SOLID,Color.YELLOW,null);

        listData = new ArrayList<PointInfoDao>();

        Fragment fragment;
        fragment = getSupportFragmentManager().findFragmentById(R.id.fragment);

        recyclerView = fragment.getView().findViewById(R.id.search_recyclerview);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        int space = 10;
        recyclerView.addItemDecoration(new SpacesItemDecoration(space));
        listMessageAdapter = new ListMessageAdapter(this, listData);
        recyclerView.setAdapter(listMessageAdapter);
        setAdapterListener();

        mMapView.setOnTouchListener(new GeoTrackTouchListener(MainActivity.this,mMapView));




        // 数据采集功能 待完成内容项
        // 1.点击地图获取当前地图点经纬度坐标 支持输入信息如：当前地址，管理员，身份证号，手机号，照片拍摄保存等
        // 2.支持拖动地图改动当前采集点位置，和拖动当前采集点改动采集点位置两种模式，实现修改当前点位置功能。
        // 3.支持数据入库oracle,postgrsql两种模式，可切换
        // 4.支持采集点信息内容列表展示，同时点上图，列表支持及单个及多个内容增删改
        // 5.支持点击地图上点，显示气泡及简要信息，可查看详细信息，及内容增删改
        // 6.支持数据库模糊搜索查看，列表展示及展示后上图，列表增删改
        // 7.点图标，字段内容，数据库模糊搜索，数据库入库等支持配置项
        // 拓展项：  支持点线面上图及编辑  具体实现功能内容如上。
    }

    /**
     *获取集合图形的中心点
     * @param geometry
     * @return
     */
    public  Point getCenter(Geometry geometry){
        if (geometry instanceof Polygon){
            Polygon polygon = (Polygon)geometry;
            Envelope envelope = polygon.getExtent();
            return  envelope.getCenter();
        }else if(geometry instanceof Polyline){
            Polyline polyline = (Polyline)geometry;
            return  polyline.getExtent().getCenter();
        }

        return null;
    }

    public void setAdapterListener(){
        listMessageAdapter.setOnLongClickListener(new ListMessageAdapter.onLongClick() {
            @Override
            public void onLongClick(View view,int position) {
                showPopupMenu(view,position);
            }
        });
        listMessageAdapter.setOnitemClickLintener(new ListMessageAdapter.OnitemClick() {
            @Override
            public void onItemClick(int position) {
                currentPoint = listData.get(position);
                if (currentPoint.getAdmin() == 0){
                    PointInfoDao dao = listData.get(position);
                    showCallout(dao);
                }
                else if(currentPoint.getAdmin() == 2 || currentPoint.getAdmin() == 1){
//                    PointInfoDao dao = listData.get(position);
                    showLineAndAreaCallout(currentPoint);
                }
            }
        });
    }

    public void showCallout( PointInfoDao dao){
        LayoutInflater infalter = LayoutInflater.from(MainActivity.this);
        View view = infalter.inflate(R.layout.callout_layout, null);
        ImageView imageView = (ImageView)view.findViewById(R.id.close);
        LinearLayout layout_main = (LinearLayout) view.findViewById(R.id.callout);
        TextView location = (TextView) view.findViewById(R.id.geo_callout_location);
        TextView idCard = (TextView) view.findViewById(R.id.geo_callout_idCard);
        TextView phonenumber = (TextView)view.findViewById(R.id.geo_callout_phonenumber);
        ImageView showImage = (ImageView)view.findViewById(R.id.callout_image);
        location.setText("当前位置: " + dao.getCurrentLocation());
        idCard.setText("身份证号码: " + dao.getIdCard());
        phonenumber.setText("电话号码: " + dao.getPhonenumber());
        Point point_ags = new Point(dao.getLongtitude(), dao.getLatitude());
        wgs84Point = (Point) GeometryEngine.project(point_ags, SpatialReferences.getWgs84());
        // get callout, set content and show
        if (dao.getFilepath() != null && dao.getFilepath().size() > 0){
            showImage.setVisibility(View.VISIBLE);
            String urlImage = Urls.URL_IMAGE + dao.getFilepath().get(0);
            Glide.with(this).load(urlImage).into(showImage);
        }else{
            showImage.setVisibility(View.GONE);
        }

        mCallout = mMapView.getCallout();
        mCallout.getStyle().setBorderWidth(1);
        mCallout.setLocation(wgs84Point);
        mCallout.setContent(view);
        mCallout.show();
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCallout.dismiss();
            }
        });

        layout_main.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, PointInputActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("point", currentPoint);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        // center on tapped point
        mMapView.setViewpointCenterAsync(wgs84Point);
    }



    public void showLineAndAreaCallout(PointInfoDao dao){
        Geometry geometry = Geometry.fromJson(currentPoint.getGeometry());
//                    Envelope envelope = (Envelope)geometry;
        Graphic graphic = new Graphic(geometry);
        Point point = getCenter(geometry);
        mMapView.setViewpointCenterAsync(point);
        LayoutInflater infalter = LayoutInflater.from(MainActivity.this);
        View view = infalter.inflate(R.layout.callout_layout, null);
        LinearLayout layout_main = (LinearLayout) view.findViewById(R.id.callout);
        ImageView imageView = (ImageView)view.findViewById(R.id.close);
        TextView location = (TextView) view.findViewById(R.id.geo_callout_location);
        TextView idCard = (TextView) view.findViewById(R.id.geo_callout_idCard);
        TextView phonenumber = (TextView)view.findViewById(R.id.geo_callout_phonenumber);
        ImageView showImage = (ImageView)view.findViewById(R.id.callout_image);

        if (dao.getFilepath() != null && dao.getFilepath().size() > 0){
            showImage.setVisibility(View.VISIBLE);
            String urlImage = Urls.URL_IMAGE + dao.getFilepath().get(0);
            Glide.with(this).load(urlImage).into(showImage);
        }else{
            showImage.setVisibility(View.GONE);
        }
        location.setText("当前位置: " + dao.getCurrentLocation());
        idCard.setText("身份证号码: " + dao.getIdCard());
        phonenumber.setText("电话号码: " + dao.getPhonenumber());
        mCallout = mMapView.getCallout();
        mCallout.setLocation(point);
        mCallout.setContent(view);
        mCallout.show();

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCallout.dismiss();
            }
        });
        layout_main.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, PointInputActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("point", currentPoint);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

//    public void executeAsyncTask(ImageView showImage,PointInfoDao pointInfoDao){
//        String urlImage = Urls.URL_IMAGE + pointInfoDao.getFilepath().get(0);
//        BitMapWorkTask task = new BitMapWorkTask(showImage);
//        task.execute(urlImage);
//        Log.i("arcgis_image","图片加载完成");
//    }

    public void initial() {
        mMapView.getGraphicsOverlays().add(getCurPosOverlay());

    }

    public void delete() {
        if (sketchEditor != null && sketchEditor.getGeometry() != null) {
            sketchEditor.clearGeometry();
        }
    }

    public void close(){
        endDraw();
    }

    public void redo() {
        if (sketchEditor == null){
            return;
        }
        if (sketchEditor.canUndo()) {
            sketchEditor.undo();
        }
    }

    public void addMapListener(){
        mMapView.setOnTouchListener(new GeoTrackTouchListener(MainActivity.this,mMapView));
    }


    public void beginDraw() {
//        if (mMapView.isActivated())

        mMapView.setSketchEditor(getSketchEditor());

        getFragmentTool().setSketchEditor(getSketchEditor());
    }

    public void endDraw() {

        if (sketchEditor != null) {

            sketchEditor.stop();

            sketchEditor.clearGeometry();

            sketchEditor.setVisible(false);

            sketchEditor = null;

//            sketchEditor.removeGeometryChangedListener();

//            mapView.setSketchEditor(null);

            getFragmentTool().setSketchEditor(null);



        }
    }




    private void selectNetworkData(int type) {
        currentType = type;
        OkGo.<String>get(Urls.URL_Query_All)
                .params("type",type)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String str = response.body().toString();
                        System.out.println("-----------");
                        Gson gson = new Gson();
                        List<PointInfoDao> data = gson.fromJson(str, new TypeToken<List<PointInfoDao>>() {
                        }.getType());
                        System.out.println("-----------");
                        listData.clear();
                        for (int i = 0; i < data.size(); i++) {
                            listData.add(data.get(i));
                        }
                        listMessageAdapter.notifyDataSetChanged();

                        generatePoint();

                    }
                });
    }

    public void changePointMode() {
        if (getFragmentTool().pointType == FragmentTool.ToolPointType.TOOL_POINT_FREE) {
            centerImage.setVisibility(View.GONE);
        } else {
            centerImage.setVisibility(View.VISIBLE);
        }
    }

    public  void changeListDataMode(PointListFragment.ListEnumType type){
        if (mCallout!= null && mCallout.isShowing()){
            mCallout.dismiss();
        }
        switch (type){
            case ALL:
                selectNetworkData(0);
                break;
            case POINT:
                selectNetworkData(1);
                break;
            case LINE:
                selectNetworkData(2);
                break;
            case AREA:
                selectNetworkData(3);
                break;
        }
    }


    public void generatePoint(){
        getPointOverLay().getGraphics().clear();
        lineOverLay.getGraphics().clear();
        polygonOverLay.getGraphics().clear();
        SimpleMarkerSymbol simpleMarkerSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CIRCLE, Color.RED, 20);
        for (int i = 0; i < listData.size(); i++) {
            PointInfoDao dao = listData.get(i);
            if (dao.getAdmin() == 0){
                Point point_ags = new Point(dao.getLongtitude(), dao.getLatitude());
                wgs84Point = (Point) GeometryEngine.project(point_ags, SpatialReferences.getWgs84());
                Graphic graphic = new Graphic(wgs84Point, simpleMarkerSymbol);
                //清除上一个点
                getPointOverLay().getGraphics().add(graphic);
            }else if(dao.getAdmin() == 1){
                Geometry geometry = Geometry.fromJson(dao.getGeometry());
                Graphic graphic = new Graphic(geometry);
                graphic.setSymbol(mLineSymbol);

                lineOverLay.getGraphics().add(graphic);
            }else  if(dao.getAdmin() == 2){
                Geometry geometry = Geometry.fromJson(dao.getGeometry());
                Graphic graphic = new Graphic(geometry);
                graphic.setSymbol(mFillSymbol);
                polygonOverLay.getGraphics().add(graphic);
            }
        }
    }

    private void initMap() {

        //加载基础地图
        mMapView = (MapView) findViewById(R.id.mapView);
        ArcGISMap arcGISMap = new ArcGISMap();
        ArcGISTiledLayer mainArcGISVectorTiledLayer = new ArcGISTiledLayer("http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineCommunity/MapServer");

        Basemap basemap = new Basemap(mainArcGISVectorTiledLayer);

        arcGISMap.setBasemap(basemap);

        mMapView.setMap(arcGISMap);

        //设置地图初始化位置
        Viewpoint vp = new Viewpoint(40.838096, 111.677914, 20000);
        arcGISMap.setInitialViewpoint(vp);

        //去除许可信息
        String licenseCode = "runtimelite,1000,rud7659408794,none,ZZ0RJAY3FY0GEBZNR002";
        ArcGISRuntimeEnvironment.setLicense(licenseCode);
        //去除esri水印
        mMapView.setAttributionTextVisible(false);

        // 地图上加载点
        mMapView.getGraphicsOverlays().add(mGraphicsOverlay);
        mMapView.getGraphicsOverlays().add(getPointOverLay());
        mMapView.getGraphicsOverlays().add(getLineOverLay());
        mMapView.getGraphicsOverlays().add(getPolygonOverLay());
        final PictureMarkerSymbol campsiteSymbol = new PictureMarkerSymbol(
                "http://sampleserver6.arcgisonline"
                        + ".com/arcgis/rest/services/Recreation/FeatureServer/0/images/e82f744ebb069bb35b234b3fea46deae");
        //Optionally set the size, if not set the image will be auto sized based on its size in pixels,
        //its appearance would then differ across devices with different resolutions.
        campsiteSymbol.setHeight(18);
        campsiteSymbol.setWidth(18);
        campsiteSymbol.loadAsync();
        //[DocRef: END]
        campsiteSymbol.addDoneLoadingListener(new Runnable() {
            @Override
            public void run() {
                //Once the symbol has loaded, add a new graphic to the graphic overlay
                Point campsitePoint = new Point(40.838096, 111.677914, wgs84);
                Graphic campsiteGraphic = new Graphic(campsitePoint, campsiteSymbol);
                mGraphicsOverlay.getGraphics().add(campsiteGraphic);
            }
        });

    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1000 && resultCode == 1001) {
            PointInfoDao point = (PointInfoDao) data.getSerializableExtra("point");
            Log.i("12", point.getCurrentLocation());

            listData.add(point);
            listMessageAdapter.notifyDataSetChanged();

        }
    }




    @Override
    protected void onPause() {
        super.onPause();
        mMapView.pause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mMapView.resume();
        reloadNetworkData(currentType);
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMapView.dispose();
    }


    public void next() {
        if (sketchEditor == null){
            return;
        }

        if (getFragmentTool().getCurMode() == SketchCreationMode.POINT){
                if (getFragmentTool().pointType == FragmentTool.ToolPointType.TOOL_POINT_CENTER){
//                    Geometry geometry = sketchEditor.getGeometry();
                    Display display = getWindowManager().getDefaultDisplay();
                   android.graphics.Point screenPoint = new android.graphics.Point();
                   display.getSize(screenPoint);
                    Point mapPoint = mMapView.screenToLocation(new android.graphics.Point(1280,800));
                    wgs84Point = (Point) GeometryEngine.project(mapPoint, SpatialReferences.getWgs84());
                    PointInfoDao dao = new PointInfoDao(wgs84Point.getX(),wgs84Point.getY());
//                    dao.setGeometry(geometry.toJson());
                    Intent intent = new Intent();
                    intent.setClass(MainActivity.this, PointInputActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("point", dao);
                    intent.putExtras(bundle);
//                    startActivity(intent);
                    startActivityForResult(intent, 998);
                }else{
                    toPointActivity();
                }
        }else{
            toPointActivity();
        }
    }

    public void toPointActivity() {
        Geometry geometry = sketchEditor.getGeometry();

        if (geometry == null)
            return;
//        endDraw();
        Graphic graphic = new Graphic(geometry);
        String geometryStr;
        PointInfoDao point = null;
        if (graphic.getGeometry().getGeometryType() == GeometryType.POINT) {
            Point geoPoint = (Point) geometry;
            getPointOverLay().getGraphics().add(graphic);

            Point wgsPoint = (Point) GeometryEngine.project(geoPoint, SpatialReference.create(4326));
            point = new PointInfoDao(wgsPoint);
            geometryStr = graphic.getGeometry().toJson();
            point.setGeometry(geometryStr);
            System.out.println("point");

        } else if (graphic.getGeometry().getGeometryType() == GeometryType.POLYLINE) {
//            geometryStr = new Gson().toJson(graphic.getGeometry());
            graphic.setSymbol(mLineSymbol);
            lineOverLay.getGraphics().add(graphic);
            geometryStr = graphic.getGeometry().toJson();
            point = new PointInfoDao(1, geometryStr);
        } else {
            graphic.setSymbol(mFillSymbol);
            polygonOverLay.getGraphics().add(graphic);
            geometryStr = graphic.getGeometry().toJson();
            point = new PointInfoDao(2, geometryStr);
            System.out.println("point");
        }

        delete();
//        PointInfoDao point =  listData.get(position);
        Intent intent = new Intent();
        intent.setClass(MainActivity.this, PointInputActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("point", point);
        intent.putExtras(bundle);
        startActivityForResult(intent, 998);
    }

    public void search(String text) {
        OkGo.<String>get(Urls.URL_Query_S)
                .params("text", text)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String str = response.body().toString();
                        System.out.println("-----------");
                        Gson gson = new Gson();
                        List<PointInfoDao> data = gson.fromJson(str, new TypeToken<List<PointInfoDao>>() {
                        }.getType());
                        System.out.println("-----------");
                        for (int i = 0; i < listData.size(); i++) {

                            listData.remove(i);
                        }
                        for (int i = 0; i < data.size(); i++) {
                            listData.add(data.get(i));
                        }
                        listMessageAdapter.notifyDataSetChanged();

                    }
                });
    }

    public void showPopupMenu(View view,int position){
        showTwo(position);
    }

    private void showTwo(final int position) {
        final PointInfoDao info = listData.get(position);
        builder = new AlertDialog.Builder(this).setIcon(R.mipmap.ic_launcher).setTitle("确定要删除吗？")
                .setMessage(info.getCurrentLocation()).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //ToDo: 你想做的事情
                        deleteData(info.getId());
                        listData.remove(position);
//                        Toast.makeText(MainActivity.this, "确定按钮", Toast.LENGTH_LONG).show();
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //ToDo: 你想做的事情
//                        Toast.makeText(MainActivity.this, "关闭按钮", Toast.LENGTH_LONG).show();
                        dialogInterface.dismiss();
                    }
                });
        builder.create().show();
    }

    public void reloadNetworkData(int type){
            selectNetworkData(currentType);
    }

    public void showCalloutPoint(String  findMap){
        if (mCallout != null && mCallout.isShowing()){
            mCallout.dismiss();
        }

        for(int index = 0;index< listData.size();index++){
            PointInfoDao dao = listData.get(index);
            if (dao.getAdmin() != 0){
                continue;
            }

//            Geometry geometry = Geometry.fromJson(dao.getGeometry());
//            Point geometry_4326 = (Point) GeometryEngine.project(geometry,SpatialReference.create(4326));

            Point secondGeo = (Point) Geometry.fromJson(findMap);
            if (Math.abs(dao.getLongtitude() - secondGeo.getX()) < 0.003 || Math.abs(dao.getLatitude() -secondGeo.getY()) < 0.003){
                currentPoint = dao;
                showCallout(dao);
                return;
            }


        }
    }

    public void showCalloutLine(String findMap){
        if (mCallout != null && mCallout.isShowing()){
            mCallout.dismiss();
        }
        for(int index = 0;index< listData.size();index++){
            PointInfoDao dao = listData.get(index);
            if (dao.getAdmin() != 1){
                continue;
            }

            if (dao.getGeometry().equals(findMap)){
                currentPoint = dao;
                showLineAndAreaCallout(dao);
                return;
            }


        }
    }

    public void showCalloutPolygon(String findMap){
        if (mCallout != null && mCallout.isShowing()){
            mCallout.dismiss();
        }
        for(int index = 0;index< listData.size();index++){
            PointInfoDao dao = listData.get(index);
            if (dao.getAdmin() != 2){
                continue;
            }

            if (dao.getGeometry().equals(findMap)){
                currentPoint = dao;
                showLineAndAreaCallout(dao);
                return;
            }


        }
    }

    public void deleteData(int id){
        OkGo.<String>post(Urls.URL_Delete_Point)
                .params("id",id)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        listMessageAdapter.notifyDataSetChanged();
                        generatePoint();
                        if (mCallout != null && mCallout.isShowing()){
                            mCallout.dismiss();
                        }
                        Toast.makeText(MainActivity.this,"删除成功",Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                    }
                });
    }
}
